package cn.zxy.config;

import cn.hutool.json.JSONUtil;
import cn.zxy.exception.BizException;
import cn.zxy.model.ResponseDTO;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestControllerAdvice
//第四种方式
public class MyRestControllerAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    //beforeBodyWrite 在controller 返回视图之前，会执行MethodParameter 获取方法的参数类型
    //body 响应体 对应的响应内容
    //returnType 获取方法的参数类型
    //selectedContentType 媒体类型：决定浏览器以什么形式什么编码的解析显示
    //selectedConverterType 信息转换器
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        if (body instanceof ResponseDTO){
            return body;
        }
        //正常返回数据

        ResponseDTO success = ResponseDTO.success(body);
        //return success;

        //针对String返回值  无法对StringHttpMessageConverter进行
        if (selectedConverterType== StringHttpMessageConverter.class){
            return JSONUtil.toJsonStr(success);
        }else {
            return success;
        }

        //解套 解决实体的另一层
        //return null;
    }

    //使用统一返回值处理异常
    @ExceptionHandler(Exception.class)
    public ResponseDTO error(Exception ex){
//        ResponseDTO dto = new ResponseDTO();
        int code=909;
        if (ex instanceof BizException){
            BizException bizException =(BizException) ex;
            code=bizException.getCode();
        }
        return ResponseDTO.error(code,ex.getMessage());
    }








//public class MyRestControllerAdvice {
//
//    @ExceptionHandler(Exception.class)
//    public ResponseDTO error(Exception ex){
////        ResponseDTO dto = new ResponseDTO();
//        int code=909;
//        if (ex instanceof BizException){
//            BizException bizException =(BizException) ex;
//            code=bizException.getCode();
//        }
//        return ResponseDTO.error(code,ex.getMessage());
//    }

//    @ExceptionHandler(Exception.class)
//    public Object error(Exception ex){
//        ResponseDTO dto = new ResponseDTO();
//        if (ex instanceof BizException){
//            BizException bizException =(BizException) ex;
//            dto.setCode(bizException.getCode());
//        }else {
//            dto.setCode(909);
//        }
//        dto.setMessage(ex.getMessage());
//        return dto;
//    }

//    @ExceptionHandler(Exception.class)
//    public Object error(Exception ex){
//        Map map = new HashMap();
//        map.put("code",1);
//        map.put("message",ex.getMessage());
//        return map;
//    }

}
